package top.moonkit.rs_pro.patterns.CheckCode;

import cn.hutool.core.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import top.moonkit.rs_pro.entity.LoginCheckCode;
import top.moonkit.rs_pro.entity.User;
import top.moonkit.rs_pro.entity.vo.LoginRequestVO;
import top.moonkit.rs_pro.exception.BusinessException;
import top.moonkit.rs_pro.service.LoginCheckCodeService;
import top.moonkit.rs_pro.utils.Mu;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author moon
 * @since
 */
@Component
public  abstract class CheckCodeService {


    @Autowired
    private LoginCheckCodeService loginCheckCodeService;


    ThreadLocal<String> ACCOUNT_CONTENT = new ThreadLocal<>();

    abstract boolean isMe(String account);

    abstract void send(String code);

    @Transactional
    public void sendCode() {
        String code = getCode()+"";
        LocalDateTime now = LocalDateTime.now();
        LoginCheckCode checkCode = LoginCheckCode.builder()
                .userAccount(ACCOUNT_CONTENT.get())
                .code(code)
                .createTime(Timestamp.valueOf(now))
                .expireTime(Timestamp.valueOf(now.plusMinutes(5)))
                .ipAddr("127.0.0.1")
                .build();
        loginCheckCodeService.save(checkCode);
        send(code);
    }
    /**
     * 获取验证码
     *
     * @return 验证码
     */
    static int getCode() {
        // 生成四位随机数
        return RandomUtil.randomInt(111111, 999999);
    }
}
